package com.o2o.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.xiatang.sphinx.Search;
import com.xiatang.weibo.action.BaseAction;

public class SphinxAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1065024789143390546L;


	private SphinxSearch search;
	private String q;

	private String sort;
	private String sortName;
	private int page = 1;
	private int pageSize = 20;
	private String index = "record"; //默认搜索新闻
	private int property = 10;
	private String groupName = "";//分组
	private String source;
	private int pid = 103;//微博pid  103新浪，104腾讯，108网易，109，搜狐
	
	public String search() {
		try {
			String table = "vyuqing_record";
			Map<String, Integer> fieldWeights = Collections.synchronizedMap(new HashMap<String, Integer>()); // 权重设置
			Map<String, Integer> param = Collections.synchronizedMap(new HashMap<String, Integer>()); // 参数设置
			if (index.equals("record")) {
				table = "vyuqing_record";
				fieldWeights.put("title", 8); //设置权重
				fieldWeights.put("content", 4);
				param.put("similar", 0); 
			}
			if(index.equals("sina_record")){
				table = "vyuqing_sina_record";
			}
			if(index.equals("tencent_record")){
				table = "vyuqing_tencent_record";
			}
			if (sort == null || sort.equals("")) {
				sort = "datedesc"; // 默认以时间倒序
			}
			if (property >= 0 && property <= 2) {
				param.put("property", property);
			}
//			if(source != null && !source.equals("")){//来源
//				param.put("source", source);
//			}
			Map<Integer,List> map= search.getSearchResult(sort, sortName, q, page, pageSize, index,
					table, param, fieldWeights,groupName);
			Set<Entry<Integer, List>> entrySet = map.entrySet();
			for (Entry<Integer, List> entry : entrySet) {
				result.put("result", entry.getValue());
				result.put("countSize", entry.getKey());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public void setQ(String q) {
		this.q = q;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public void setSortName(String sortName) {
		this.sortName = sortName;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public void setProperty(int property) {
		this.property = property;
	}

	public void setSearch(SphinxSearch search) {
		this.search = search;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

}
